home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gspath2.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  269 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gspath2.c */
  20. /* Non-constructor path routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gspath.h"
  24. #include "gxfixed.h"
  25. #include "gxarith.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzpath.h"
  29. #include "gzdevice.h"
  30.  
  31. /* Forward references */
  32. private int common_clip(P2(gs_state *, int));
  33. private int set_clip_path(P3(gs_state *, gx_clip_path *, int));
  34.  
  35. /* Path enumeration structure */
  36. struct gs_path_enum_s {
  37.     const segment *pseg;
  38.     const gs_state *pgs;
  39.     gx_path copied_path;    /* a copy of the path, release when done */
  40.     int moveto_done;    /* have we reported a final moveto yet? */
  41. };
  42.  
  43. /* Size of path enumeration structure, so clients can allocate */
  44. const uint gs_path_enum_sizeof = sizeof(gs_path_enum);
  45.  
  46. /* ------ Path transformers ------ */
  47.  
  48. int
  49. gs_flattenpath(gs_state *pgs)
  50. {    gx_path fpath;
  51.     int code;
  52.     if ( !pgs->path->curve_count ) return 0;    /* no curves */
  53.     code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  54.     if ( code < 0 ) return code;
  55.     gx_path_release(pgs->path);
  56.     *pgs->path = fpath;
  57.     return 0;
  58. }
  59.  
  60. int
  61. gs_reversepath(gs_state *pgs)
  62. {    gx_path rpath;
  63.     int code = gx_path_copy_reversed(pgs->path, &rpath, 1);
  64.     if ( code < 0 ) return code;
  65.     gx_path_release(pgs->path);
  66.     *pgs->path = rpath;
  67.     return 0;
  68. }
  69.  
  70. /* ------ Accessors ------ */
  71.  
  72. int
  73. gs_pathbbox(gs_state *pgs, gs_rect *pbox)
  74. {    gs_fixed_rect fbox;        /* box in device coordinates */
  75.     gs_rect dbox;
  76.     int code = gx_path_bbox(pgs->path, &fbox);
  77.     if ( code < 0 ) return code;
  78.     /* Transform the result back to user coordinates. */
  79.     dbox.p.x = fixed2float(fbox.p.x);
  80.     dbox.p.y = fixed2float(fbox.p.y);
  81.     dbox.q.x = fixed2float(fbox.q.x);
  82.     dbox.q.y = fixed2float(fbox.q.y);
  83.     return gs_bbox_transform_inverse(&dbox, &ctm_only(pgs), pbox);
  84. }
  85.  
  86. /* ------ Enumerators ------ */
  87.  
  88. /* Start enumerating a path */
  89. int
  90. gs_path_enum_init(gs_path_enum *penum, const gs_state *pgs)
  91. {    gx_path *ppath = pgs->path;
  92.     int code = gx_path_copy(ppath, &penum->copied_path, 1);
  93.     if ( code < 0 )
  94.         return code;
  95.     penum->pgs = pgs;
  96.     penum->pseg = (const segment *)penum->copied_path.first_subpath;
  97.     penum->moveto_done = 0;
  98.     return 0;
  99. }
  100.  
  101. /* Enumerate the next element of a path. */
  102. /* If the path is finished, return 0; */
  103. /* otherwise, return the element type. */
  104. int
  105. gs_path_enum_next(gs_path_enum *penum, gs_point ppts[3])
  106. {    const segment *pseg = penum->pseg;
  107.     const gs_state *pgs = penum->pgs;
  108.     gs_point pt;
  109.     int code;
  110.     if ( pseg == 0 )
  111.     {    /* We've enumerated all the segments, but there might be */
  112.         /* a trailing moveto. */
  113.         const gx_path *ppath = pgs->path;
  114.         const subpath *psub = ppath->current_subpath;
  115.         if ( !ppath->subpath_open && ppath->position_valid &&
  116.              !penum->moveto_done && (psub == 0 || !psub->closed ||
  117.             /* If the last subpath was closed, we can't know */
  118.             /* if it was followed by a move to the same point. */
  119.             /* Just compare the coordinates and hope we're right. */
  120.              psub->last->pt.x != ppath->position.x ||
  121.              psub->last->pt.y != ppath->position.y)
  122.            )
  123.            {    penum->moveto_done = 1;
  124.             if ( (code = gs_itransform(pgs,
  125.                 fixed2float(ppath->position.x),
  126.                 fixed2float(ppath->position.y),
  127.                 &ppts[0])) < 0 )
  128.               return code;
  129.             return gs_pe_moveto;
  130.            }
  131.         return 0;
  132.     }
  133.     penum->pseg = pseg->next;
  134.     if ( pseg->type == s_line_close )
  135.       return gs_pe_closepath;
  136.     if ( (code = gs_itransform(pgs, fixed2float(pseg->pt.x),
  137.                    fixed2float(pseg->pt.y), &pt)) < 0 )
  138.       return code;
  139.     switch ( pseg->type )
  140.        {
  141.     case s_start:
  142.          ppts[0] = pt;
  143.          return gs_pe_moveto;
  144.     case s_line:
  145.          ppts[0] = pt;
  146.          return gs_pe_lineto;
  147.     case s_curve:
  148. #define pcurve ((const curve_segment *)pseg)
  149.          if ( (code =
  150.            gs_itransform(pgs, fixed2float(pcurve->p1.x),
  151.                  fixed2float(pcurve->p1.y), &ppts[0])) < 0 ||
  152.           (code =
  153.            gs_itransform(pgs, fixed2float(pcurve->p2.x),
  154.                  fixed2float(pcurve->p2.y), &ppts[1])) < 0 )
  155.            return 0;
  156.          ppts[2] = pt;
  157.          return gs_pe_curveto;
  158. #undef pcurve
  159.     default:
  160.          lprintf1("bad type %x in gs_path_enum_next!\n", pseg->type);
  161.          return_error(gs_error_Fatal);
  162.        }
  163. }
  164.  
  165. /* Clean up after a pathforall. */
  166. void
  167. gs_path_enum_cleanup(gs_path_enum *penum)
  168. {    gx_path_release(&penum->copied_path);
  169.     gx_path_reset(&penum->copied_path);    /* don't do it twice */
  170. }
  171.  
  172. /* ------ Clipping ------ */
  173.  
  174. int
  175. gs_clippath(gs_state *pgs)
  176. {    gx_path path;
  177.     int code = gx_cpath_path(pgs->clip_path, &path);
  178.     if ( code < 0 ) return code;
  179.     return gx_path_copy(&path, pgs->path, 1);
  180. }
  181.  
  182. int
  183. gs_initclip(gs_state *pgs)
  184. {    register gx_device *dev = pgs->device->info;
  185.     gs_fixed_rect box;
  186.     if ( is_fzero2(dev->l_margin, dev->r_margin) &&
  187.          is_fzero2(dev->b_margin, dev->t_margin)
  188.        )
  189.        {    /* Shortcut, don't need to worry about density. */
  190.         box.p.x = box.p.y = 0;
  191.         box.q.x = int2fixed(dev->width);
  192.         box.q.y = int2fixed(dev->height);
  193.        }
  194.     else
  195.        {    /* Indent from bounding rectangle. */
  196.         gs_matrix_fixed imat;
  197.         (*dev->procs->get_initial_matrix)(dev, (gs_matrix *)&imat);
  198.         gs_update_matrix_fixed(&imat);
  199.         gs_point_transform2fixed(&imat,
  200.                      dev->l_margin * 72,
  201.                      dev->b_margin * 72,
  202.                      &box.p);
  203.         gs_point_transform2fixed(&imat,
  204.                      (dev->width / dev->x_pixels_per_inch - dev->r_margin) * 72,
  205.                      (dev->height / dev->y_pixels_per_inch - dev->t_margin) * 72,
  206.                      &box.q);
  207.        }
  208.     return gx_clip_to_rectangle(pgs, &box);
  209. }
  210.  
  211. int
  212. gs_clip(gs_state *pgs)
  213. {    return common_clip(pgs, gx_rule_winding_number);
  214. }
  215.  
  216. int
  217. gs_eoclip(gs_state *pgs)
  218. {    return common_clip(pgs, gx_rule_even_odd);
  219. }
  220.  
  221. private int
  222. common_clip(gs_state *pgs, int rule)
  223. {    gx_path fpath;
  224.     int code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  225.     if ( code < 0 ) return code;
  226.     code = gx_cpath_intersect(pgs, pgs->clip_path, &fpath, rule);
  227.     if ( code != 1 ) gx_path_release(&fpath);
  228.     if ( code < 0 ) return code;
  229.     return set_clip_path(pgs, pgs->clip_path, rule);
  230. }
  231.  
  232. /* Establish a rectangle as the clipping path. */
  233. /* Used by initclip and by the character cache logic. */
  234. int
  235. gx_clip_to_rectangle(gs_state *pgs, gs_fixed_rect *pbox)
  236. {    gx_clip_path cpath;
  237.     int code = gx_cpath_from_rectangle(&cpath, pbox, pgs->memory_procs);
  238.     if ( code < 0 ) return code;
  239.     gx_cpath_release(pgs->clip_path);
  240.     return set_clip_path(pgs, &cpath, gx_rule_winding_number);
  241. }
  242.  
  243. /* Set the clipping path to the current path, without intersecting. */
  244. /* Currently only used by the insideness testing operators, */
  245. /* but might be used by viewclip eventually. */
  246. /* The algorithm is very inefficient; we'll improve it later if needed. */
  247. int
  248. gx_clip_to_path(gs_state *pgs)
  249. {    gs_fixed_rect bbox;
  250.     int code;
  251.     (code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
  252.     (code = gx_clip_to_rectangle(pgs, &bbox)) < 0 ||
  253.     (code = gs_clip(pgs));
  254.     return code;
  255. }
  256.  
  257. /* Set the clipping path (internal). */
  258. private int
  259. set_clip_path(gs_state *pgs, gx_clip_path *pcpath, int rule)
  260. {    *pgs->clip_path = *pcpath;
  261.     pgs->clip_rule = rule;
  262. #ifdef DEBUG
  263. if ( gs_debug['p'] )
  264.     dprintf("[p]Clipping path:\n"),
  265.     gx_cpath_print(pcpath);
  266. #endif
  267.     return 0;
  268. }
  269.